from flask import Blueprint, render_template

from service.ArticleService import get_article_page, delete_article, publish_article, top_article, \
    get_service_article_by_uuid, get_about_by_group_id_is_about, get_article_content

admin_article = Blueprint('admin_article', __name__)
admin_article.url_prefix = '/admin'


@admin_article.route("/article_manage/<int:page>")
def get_articles(page):
    page = get_article_page(page)
    return render_template('admin/article-manage.html', page=page)


@admin_article.route('/article_edit')
def article_edit():
    return render_template('admin/edit/markdown.html')


@admin_article.route("/article_delete/<string:uuid>/<int:page>")
def article_delete(uuid, page):
    page = delete_article(uuid, page)
    return render_template('admin/article-manage.html', page=page)


@admin_article.route("/article_publish/<string:uuid>/<int:page>")
def article_publish(uuid, page):
    page = publish_article(uuid, page)
    return render_template('admin/article-manage.html', page=page)


@admin_article.route("/article_top/<string:uuid>/<int:page>")
def article_top(uuid, page):
    page = top_article(uuid, page)
    return render_template('admin/article-manage.html', page=page)


@admin_article.route("/edit_article/<string:uuid>")
def get_article_by_uuid(uuid):
    article = get_service_article_by_uuid(uuid)
    article.content = get_article_content(article)
    return render_template('admin/article/edit-markdown.html', article=article)


@admin_article.route("/article_edit_about_me")
def get_about_me_article():
    article = get_about_by_group_id_is_about()
    if article is None:
        return "error"
    article.content = get_article_content(article)
    return render_template('admin/article/edit-about.html', article=article)
